package com.slg.android.manyanime.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.adapter.CartoonGroupAdapter;
import com.slg.android.manyanime.bean.CartoonGroup;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Thinkpad on 2015/3/11.
 */
public class CartoonCategoryFragment extends Fragment {

    public static String path =
            "http://manhua.api.61.com/?&pf=1&android_version=17&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Cartoon&a=channelgroup";
    @ViewInject(R.id.fragment_cartoon_category_list)
    private ListView cateList;

    private List<CartoonGroup> groups;

    public CartoonCategoryFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cartoon_category, container, false);
        ViewUtils.inject(this, view);
        groups = new LinkedList<CartoonGroup>();
        HttpUtils httpUtils = new HttpUtils();

        final CartoonGroupAdapter adapter = new CartoonGroupAdapter(getActivity(), groups);
        cateList.setAdapter(adapter);
        httpUtils.send(HttpRequest.HttpMethod.GET, path,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                        String str = stringResponseInfo.result;
                        if (str != null) {
                            try {
                                JSONArray jsonArray = new JSONArray(str);
                                parseData(jsonArray);
                                adapter.notifyDataSetChanged();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(getActivity(), R.string.no_network, Toast.LENGTH_LONG).show();
                    }
                }

        );

        return view;
    }

    public void parseData(JSONArray jsonArray) throws JSONException {
        //这个返回的是一个json数组
        for (int i = 0; i < jsonArray.length(); i++) {
            CartoonGroup group = new CartoonGroup();
            JSONObject groupJson = jsonArray.getJSONObject(i);
            group.parseJson(groupJson);
            groups.add(group);
        }
    }

}
